package PaperExperiment;

import java.util.ArrayList;
import java.util.HashMap;

public class RuleTree {
	protected ArrayList<Rule> listRule = null;
	
	public RuleTree() {
		this.listRule = new ArrayList<Rule>();
	}
	
	public void ACT001_addRule(int cellNRow, int cellNCol, CellGroup gIn, CellGroup gOut, 
			int minPassCount, ArrayList<TrajRegion> listTrajRegion, Cell[][] mapCell) {
		
		Rule newRule = new Rule(gIn, gOut);
		
		int indexTraj = 0;
		int[][] cellCount = new int[cellNRow][cellNCol];
		for(int row = 0; row < cellCount.length; row++) {
			for(int col = 0; col < cellCount[0].length; col++) {
				cellCount[row][col] = 0;
			}
		}
		
		while(indexTraj < listTrajRegion.size()) {
			ArrayList<Cell> getListTrajCell = listTrajRegion.get(indexTraj).GET001_ListCoverCellSeqly();
			HashMap<String, String> hashStored = new HashMap<String, String>();
			//check whether start and end are belongs to current in-out group
			if(gIn.ACT003_isBelongGroup(getListTrajCell.get(0)) && gOut.ACT003_isBelongGroup(getListTrajCell.get(getListTrajCell.size() - 1))) {
				int indexPoint = 1;
				while(indexPoint < getListTrajCell.size() - 1) {
					int curRow = getListTrajCell.get(indexPoint).row;
					int curCol = getListTrajCell.get(indexPoint).col;
					//check repeatedly passing
					if(!hashStored.containsKey(Integer.toString(curRow) + Integer.toString(curCol))) {
						cellCount[curRow][curCol] ++;
						if(cellCount[curRow][curCol] >= minPassCount) {
							newRule.ACT001_addCell(mapCell[curRow][curCol]);
						}
						hashStored.put(Integer.toString(curRow) + Integer.toString(curCol), "");
					}
					indexPoint = indexPoint + 1;
				}
			}
			indexTraj = indexTraj + 1;
		}
		this.listRule.add(newRule);
	}
	
	public boolean ACT002_isAbnormalTraj(TrajRegion tr) {
		boolean isAbnormal = true;
		int index = 0;
		while(isAbnormal && index < this.listRule.size()) {
			if(this.listRule.get(index).ACT003_isBelongRule(tr)) {
				isAbnormal = false;
			}
			index = index + 1;
		}
		return isAbnormal;
	}
	
	public ArrayList<Rule> GET001_ListRule() {
		return this.listRule;
	}
}
